#ifndef sonar_h
#define sonar_h

/**
 * @defgroup nxbot_sonar SONAR Library
 * @ingroup nxbot_low_level
 * @code #include <sonar.h> @endcode
 * @brief SONAR library for reading SRF02 sonar-modules, connected via serial communication. 
 *
 * This module uses UART0 of the ATmega1281 microcontroller for communicating with the SRF02 modules. It can be configured for 3 to 8 SRF02 modules,
 * all sharing the same serial connection. It is necessary to configure different directions for each module.
 * This library is not an interrupt-driven module, but, as it depends on uart configuration, which is indeed an interrupt-driven module, it is 
 * necessary to enable interrupts by using the sei() function.
 * 
 */
 
/**@{*/

//TODO: Realizar la documentacion externa para que aparezcan estas variables
#if NXBOT_SONAR_COUNT == 3
#define SONAR_45N 0
#define SONAR_0 	1
#define SONAR_45P 2
#elif NXBOT_SONAR_COUNT == 5
#define SONAR_80N 0
#define SONAR_45N 1
#define SONAR_0 	2
#define SONAR_45P 3
#define SONAR_80P 4
#elif NXBOT_SONAR_COUNT == 6
#define SONAR_80N 0
#define SONAR_45N 1
#define SONAR_10N 2
#define SONAR_10P 3
#define SONAR_45P 4
#define SONAR_80P 5
#elif NXBOT_SONAR_COUNT == 8
#define SONAR_80N 0
#define SONAR_60N 1
#define SONAR_30N 2
#define SONAR_10N 3
#define SONAR_10P 4
#define SONAR_30N 5
#define SONAR_60P 6
#define SONAR_80P 7
#endif

/**
 * @brief State 0 of state-machine for reading distance values. 
 * @see sonar_read
 */
#define SONAR_STATE0_REQ 0
/**
 * @brief State 1 of state-machine for reading distance values.
 * @see sonar_read
 */
#define SONAR_STATE1_1B 1
/**
 * @brief State 2 of state-machine for reading distance values.
 * @see sonar_read
 */
#define SONAR_STATE2_2B 2

/**
 * @brief A distance-value has been correctly received.
 * @see sonar_read
 */
#define SONAR_READ_READY 0
/**
 * @brief No distance-value has been yet completely received.
 * @see sonar_read
 */
#define SONAR_READ_NOT_READY 1

#define SONAR_INCH 	0
#define SONAR_CM 		1
#define SONAR_US 		2

/**
 * @brief Hardware initialization of the %sonar module.
 *
 * Configures UART0 hardware in order to send and receive bytes via this interface.
 *
 */
void sonar_init(void );

/**
 * @brief Cleans uart stack.
 *
 * Normally used when a valid reading has been made. As the SRF02 does not implement any protocol or datagram for sending the reading, this function
 * is useful in order to avoid reading mistakes between multiple modules.
 *
 */
void sonar_cleanBuffer(void);

/**
 * @brief Sends command to desired SRF02 module for distance measuring.
 *
 * It sends command 0x51 for distance measuring by the desired SRF02 sensor.
 * @param sonarNum Desired SRF02 module. Use SONAR_(ANGLE) constants for readings, e.g. SONAR_45N.
 * @param units The desired units for the reading. Options are: SONAR_INCH, SONAR_CM, SONAR_US
 *
 */
void sonar_write(unsigned char sonarNum, unsigned char units);

/**
 * @brief Sends command to desired SRF02 module for distance reading.
 *
 * It sends command 0x5E for returning the distance reading of the desired SRF02 sensor. This function implements a state machine for non-blocking 
 * calls.
 * @param sonarNum Desired SRF02 module. Use SONAR_(ANGLE) constants for readings, e.g. SONAR_45N.
 * @param val Pointer where the reading will be saved.
 * @newState For cases where the state-machine has to be resetted, use this parameter, with one of the SONAR_STATE(*) constants, normalle
 * SONAR_STATE0_REQ is used. When not interested in resetting or setting the state-machine, use -1.
 */
unsigned char sonar_read(unsigned char sonarNum, unsigned int *val, char newState);

/**@}*/
#endif
